﻿<Window
		xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
		xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
		xmlns:l="clr-namespace:Gildor.SimpleHostMonitor.Desktop"
        xmlns:lc="clr-namespace:Gildor.SimpleHostMonitor.Desktop.Controls"
        xmlns:forms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
		xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero" x:Class="Gildor.SimpleHostMonitor.Desktop.Main"
		Title="Simple HostMonitor" Width="337" 
        
        Icon="/Gildor.SimpleHostMonitor.Desktop;component/Resources/64.ico" SizeToContent="Height">
    <Window.Resources>
        <l:StringToBoolNotEmptyConverter x:Key="strNotEmptyConv" />
        <l:IntToBoolNotEqualsConverter x:Key="intNotEquConv" />
        <l:NotifyStrategyToToggleButtonStateConventer x:Key="notifyStrategyToToggleButtonStateConv" />
        <l:NotifyStrategyToIndexConventer x:Key="notifyStrategyToIndexConv" />
        
        <DataTemplate x:Key="listViewHeaderTemplateAscendingSorting">
            <DockPanel>
                <TextBlock Text="{Binding}"/>
                <Path x:Name="arrow"
                StrokeThickness = "1"
                Fill = "gray"
                Data = "M 5,10 L 15,10 L 10,5 L 5,10"/>
            </DockPanel>
        </DataTemplate>

        <DataTemplate x:Key="listViewHeaderTemplateDescendingSorting">
            <DockPanel>
                <TextBlock Text="{Binding ''}"/>
                <Path x:Name="arrow"
                StrokeThickness = "1"                                   
                Fill            = "gray"
                Data            = "M 5,5 L 10,10 L 15,5 L 5,5"/>
            </DockPanel>
        </DataTemplate>

        <DataTemplate x:Key="listViewHeaderTemplateNoSorting">
            <DockPanel>
                <TextBlock Text="{Binding ''}"/>
            </DockPanel>
        </DataTemplate>
        <DataTemplate x:Key="NotifyStrategyCell">
            <StackPanel Orientation="Horizontal">
                <ToggleButton  IsThreeState="True" 
                    Foreground="{Binding Foreground, RelativeSource={RelativeSource AncestorType={x:Type ListViewItem}}}"
                    Background="{Binding Background, RelativeSource={RelativeSource AncestorType={x:Type ListViewItem}}}"
                    IsChecked="{Binding DataContext.NotifyStrategy, Converter={StaticResource notifyStrategyToToggleButtonStateConv}, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type ListViewItem}}}"
                    ToolTip="Click to change the situation when a tooltip should be displayed.">
                    <ToggleButton.Style>
                        <Style BasedOn="{StaticResource {x:Static ToolBar.ToggleButtonStyleKey}}" TargetType="{x:Type ToggleButton}">
                            <Setter Property="Content" Value="False"/>
                            <Style.Triggers>
                                <Trigger Property="IsChecked" Value="True">
                                    <Setter Property="Content" Value="Success"/>
                                </Trigger>
                                <Trigger Property="IsChecked" Value="False">
                                    <Setter Property="Content" Value="Fail"/>
                                </Trigger>
                                <Trigger Property="IsChecked" Value="{x:Null}">
                                    <Setter Property="Content" Value="Never" />
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </ToggleButton.Style>
                </ToggleButton>
            </StackPanel>
        </DataTemplate>

        <ControlTemplate x:Key="ToggleButtonToButtonTemplate" TargetType="{x:Type ButtonBase}">
            <Microsoft_Windows_Themes:ButtonChrome x:Name="Chrome" SnapsToDevicePixels="True" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" RenderDefaulted="{TemplateBinding Button.IsDefaulted}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}">
                <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" ContentTemplate="{TemplateBinding ContentTemplate}" RecognizesAccessKey="True"/>
            </Microsoft_Windows_Themes:ButtonChrome>
            <ControlTemplate.Triggers>
                <Trigger Property="IsKeyboardFocused" Value="True">
                    <Setter Property="RenderDefaulted" TargetName="Chrome" Value="True"/>
                </Trigger>
                <!--<Trigger Property="ToggleButton.IsChecked" Value="True">
                					<Setter Property="RenderPressed" TargetName="Chrome" Value="True"/>
                				</Trigger>-->
                <Trigger Property="IsEnabled" Value="False">
                    <Setter Property="Foreground" Value="#FFADADAD"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
        
        <!--Define the style for each row of host including
        triggers that change the color scheme of a cell
        when it is selected or the mouse passes over it-->
        <Style x:Key="HostListView" TargetType="{x:Type ListViewItem}">

            <Setter Property="Margin" Value="0,1,0,0"/>
            <Setter Property="Height" Value="30"/>

            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="true">
                    <Setter Property="Foreground" Value="Blue" />
                    <Setter Property="Cursor" Value="Hand"/>
                </Trigger>
                <MultiTrigger>
                    <MultiTrigger.Conditions>
                        <Condition Property="IsSelected" Value="true" />
                        <Condition Property="Selector.IsSelectionActive" Value="true" />
                    </MultiTrigger.Conditions>
                    <Setter Property="Foreground" Value="Yellow" />
                </MultiTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <lc:SystemTrayIcon x:Name="_trayIcon"
            Text="Simple HostMonitor" 
            Icon="Resources\32.ico"
            MouseDoubleClick="OnSystemTrayIconDoubleClick" 
            BalloonTipClicked="OnSystemTrayBalloonTipClicked"
            BalloonTipTitle="Simple HostMonitor"
            BalloonTipTextFormat="Ping site {0} {1}: {2}"
            BalloonTipTimeout="3000">
            <lc:SystemTrayIcon.MenuItems>
                <forms:MenuItem Text="Open" Click="OnMenuItemOpenClick" DefaultItem="True" />
                <forms:MenuItem Text="-" />
                <forms:MenuItem Text="About" Click="OnMenuItemAboutClick" />
                <forms:MenuItem Text="-" />
                <forms:MenuItem Text="Exit" Click="OnMenuItemExitClick"/>
            </lc:SystemTrayIcon.MenuItems>
        </lc:SystemTrayIcon>

        <Grid DataContext="{Binding Config, RelativeSource={RelativeSource AncestorType={x:Type Window}}}">
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <Grid Grid.Row="0">
                <ListView x:Name="_hostListView" 
                          SelectionMode="Single"
                          ItemsSource="{Binding Path=Hosts}" 
                          ItemContainerStyle="{StaticResource HostListView}"
                          VirtualizingStackPanel.IsVirtualizing="True"
                          l:ListViewSorter.CustomListViewSorter="Gildor.SimpleHostMonitor.Desktop.HostInfoComparer" Margin="0,0,0,63">
                    <ListView.View>
                        <GridView AllowsColumnReorder="True">
                            <GridViewColumn Header="Site" Width="150"  DisplayMemberBinding="{Binding Address}"/>
                            <GridViewColumn Header="State" Width="70" DisplayMemberBinding="{Binding State}"/>
                            <GridViewColumn Header="NotifyWhen" CellTemplate="{StaticResource NotifyStrategyCell}" Width="70" />
                        </GridView>
                    </ListView.View>
                    <ListView.ContextMenu>
                        <ContextMenu>
                            <MenuItem Header="Delete" Click="OnDeleteHost" />
                        </ContextMenu>
                    </ListView.ContextMenu>
                </ListView>

                <Label Content="Site" Height="25" HorizontalAlignment="Left" Margin="0,0,0,32" VerticalAlignment="Bottom" Width="50" Grid.RowSpan="2" />
                <TextBox x:Name="_siteAddress" Margin="50,0,12,35" Height="22" VerticalAlignment="Bottom" GotFocus="OnTextBoxGotFocus"
                        Text="{Binding SelectedItem.Address, ElementName=_hostListView, Mode=TwoWay, UpdateSourceTrigger=Explicit}" Grid.RowSpan="2" />
                <ToggleButton x:Name="_disableAlertCheckBox" Height="29" 
                        HorizontalAlignment="Right" Margin="0,0,219,0" 
                        VerticalAlignment="Bottom" Width="92" 
                        IsChecked="{Binding Path=IsAlertEnabled}"
                        Grid.Row="1" Template="{StaticResource ToggleButtonToButtonTemplate}">
                	<ToggleButton.Style>
                        <Style  TargetType="{x:Type ToggleButton}">
                            <Setter Property="Content" Value="Disable Alert" />
                            <Style.Triggers>
                                <Trigger Property="IsChecked" Value="False">
                                    <Setter Property="Content" Value="Enable Alert" />
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </ToggleButton.Style>
                </ToggleButton>
                <Button Margin="0,0,146,0" HorizontalAlignment="Right" Width="67" Height="29" VerticalAlignment="Bottom" Click="OnAddHost" 
                        ToolTip="Adds the inputed host."
                        IsEnabled="{Binding Text, Converter={StaticResource strNotEmptyConv}, ElementName=_siteAddress}" Grid.Row="1" Content="Add"/>
                <Button Height="29" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="67" Margin="0,0,73,0" Click="OnDeleteHost" 
                        ToolTip="Deletes the selected host."
                        IsEnabled="{Binding Text, Converter={StaticResource strNotEmptyConv}, ElementName=_siteAddress}" Grid.Row="1" Content="Delete"/>
                <Button Height="29" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="67" Click="OnUpdateHost" 
                        ToolTip="Updates the host list."
                        IsEnabled="{Binding SelectedIndex, Converter={StaticResource intNotEquConv}, ConverterParameter=-1, ElementName=_hostListView}" Grid.Row="1" Content="Update"/>
            </Grid>
            <Expander Header="Option" Grid.Row="1" IsExpanded="False">
                <Grid>
                    <TabControl>
                        <TabItem Header="Default" DataContext="{Binding Path=Default}">
                            <Grid>
                                <TextBlock Height="25" Margin="6,6,0,0" Width="132" VerticalAlignment="Top" HorizontalAlignment="Left"><Run Text="Default to notify when:"/></TextBlock>
                                <ComboBox Height="25" Margin="144,6,0,0" Width="73" VerticalAlignment="Top" HorizontalAlignment="Left"
                                        SelectedIndex="{Binding NotifyStrategy, Converter={StaticResource notifyStrategyToIndexConv}, Mode=TwoWay}">
                                	<ComboBoxItem IsSelected="True" Content="Fail"/>
                                	<ComboBoxItem Content="Success"/>
                                	<ComboBoxItem Content="Never"/>
                                </ComboBox>
                                <TextBlock Margin="6,44,0,0" Width="151" Height="25" 
                                        VerticalAlignment="Top" HorizontalAlignment="Left">
                                    <Run Text="Default to notify after failing"/>
                                </TextBlock>
                                <lc:NumberTextBox AutoWordSelection="True" Height="25" 
                                        Margin="162,44,0,0" Width="31" 
                                        VerticalAlignment="Top" HorizontalAlignment="Left"
                                        Text="{Binding FailureThreshold, Mode=TwoWay}" />
                                <TextBlock Margin="199,44,0,0" Height="25" Width="40" 
                                        HorizontalAlignment="Left" VerticalAlignment="Top">
                                    <Run Text="times."/>
                                </TextBlock>
                            </Grid>
                        </TabItem>
                    </TabControl>
                </Grid>
            </Expander>
        </Grid>
    </Grid>
</Window>
